﻿using DataTurnClient.Interface;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataTurnClient.Server
{
    public class TimeService
    {
        public TimeService()
        {
        }
        public int hour = 0;
        public int minute = 1;
        public int seconds = 2;
        public int  replenish = 3;
        public bool IsTime(List<JobTime> jobTimeList, List<string> newTime)
        {
            bool isCan = false;
            foreach (var item in jobTimeList )
            {
                if (item.IsStart == 1 && item.Index != replenish)
                {
                    FixedType(jobTimeList, newTime, item, isCan, out isCan);
                    //固定类型就代表指定时间运行
                }
                if (item.IsStart == 2 && item.Index != replenish)
                {
                    CycleType(jobTimeList,newTime,item,isCan ,out isCan);
                    //固定类型
                }
                if (item.IsStart == 3 && item.Index != replenish)
                {
                    switch (item.Index)
                    {
                        //10点代表T，11点代表E，12点是0
                        case 0:
                            {
                                if (Convert.ToInt32(item.JobRunTime.Substring(0,1)) < Convert.ToInt32( newTime[hour]) && Convert.ToInt32(newTime[hour]) < Convert.ToInt32( item.JobRunTime.Substring(1, 1)) + 12 )
                                {
                                    List<JobTime> newTimeList = new List<JobTime>();
                                    var replenishTime = new JobTime()
                                    {
                                        Index = item.Index,
                                        IsStart = jobTimeList[replenish].IsStart,
                                        JobRunTime = jobTimeList[replenish].JobRunTime
                                    };
                                    newTimeList.Add(replenishTime);
                                    newTimeList.Add(jobTimeList[minute]);
                                    newTimeList.Add(jobTimeList[seconds]);
                                    if (jobTimeList[replenish].IsStart == 1)
                                    {
                                        FixedType(newTimeList, newTime, replenishTime, isCan, out isCan);
                                    }
                                    if (jobTimeList[replenish].IsStart == 2)
                                    {
                                        CycleType(newTimeList, newTime, replenishTime, isCan, out isCan);
                                    }
                                }
                                break;
                            }
                        case 1:
                            {
                                if (Convert.ToInt32(item.JobRunTime.Substring(0, 1)) < Convert.ToInt32(newTime[hour]) && Convert.ToInt32(newTime[hour]) < Convert.ToInt32(item.JobRunTime.Substring(1, 1)) + 12)
                                {
                                    List<JobTime> newTimeList = new List<JobTime>();
                                    var replenishTime = new JobTime() {
                                       Index = item.Index,
                                       IsStart =  jobTimeList[replenish].IsStart,
                                       JobRunTime = jobTimeList[replenish].JobRunTime
                                    };
                                    newTimeList.Add(jobTimeList[hour]);
                                    newTimeList.Add(replenishTime);
                                    newTimeList.Add(jobTimeList[seconds]);
                                    if (jobTimeList[replenish].IsStart == 1)
                                    {
                                        FixedType(newTimeList, newTime, replenishTime, isCan, out isCan);
                                    }
                                    if (jobTimeList[replenish].IsStart == 2)
                                    {
                                        CycleType(newTimeList, newTime, replenishTime, isCan, out isCan);
                                    }
                                }
                                break;
                            }
                        case 2:
                            {
                                if (Convert.ToInt32(item.JobRunTime.Substring(0, 1)) < Convert.ToInt32(newTime[hour]) && Convert.ToInt32(newTime[hour]) < Convert.ToInt32(item.JobRunTime.Substring(1, 1)) + 12)
                                {
                                    List<JobTime> newTimeList = new List<JobTime>();
                                    var replenishTime = new JobTime()
                                    {
                                        Index = item.Index,
                                        IsStart = jobTimeList[replenish].IsStart,
                                        JobRunTime = jobTimeList[replenish].JobRunTime
                                    };
                                    newTimeList.Add(jobTimeList[hour]);
                                    newTimeList.Add(jobTimeList[minute]);
                                    newTimeList.Add(replenishTime);
                                    if (jobTimeList[replenish].IsStart == 1)
                                    {
                                        FixedType(newTimeList, newTime, replenishTime, isCan, out isCan);
                                    }
                                    if (jobTimeList[replenish].IsStart == 2)
                                    {
                                        CycleType(newTimeList, newTime, replenishTime, isCan, out isCan);
                                    }
                                }
                                break;
                            }
                    }
                    //固定类型就代表指定时间运行
                }
            }
            return isCan;
        }

        private void CycleType(List<JobTime> jobTimeList, List<string> newTime, JobTime item,bool inIsCan, out bool isCan)
        {
            isCan = inIsCan;
            switch (item.Index)
            {
                case 0:
                    {
                        if (Math.Round(Convert.ToDecimal(newTime[item.Index]) / Convert.ToDecimal(item.JobRunTime)) == (Convert.ToDecimal(newTime[item.Index]) / Convert.ToDecimal(item.JobRunTime))
                            && newTime[minute] == jobTimeList[minute].JobRunTime && newTime[seconds] == jobTimeList[seconds].JobRunTime)
                        {
                            isCan = true;
                        }
                        break;
                    }
                case 1:
                    {
                        if (Math.Round(Convert.ToDecimal(newTime[item.Index]) / Convert.ToDecimal(item.JobRunTime)) == (Convert.ToDecimal(newTime[item.Index]) / Convert.ToDecimal(item.JobRunTime))
                            && newTime[seconds] == jobTimeList[seconds].JobRunTime)
                        {
                            isCan = true;
                        }
                        break;
                    }
                case 2:
                    {
                        if (Math.Round(Convert.ToDecimal(newTime[item.Index]) / Convert.ToDecimal(item.JobRunTime)) == (Convert.ToDecimal(newTime[item.Index]) / Convert.ToDecimal(item.JobRunTime)))
                        {
                            isCan = true;
                        }
                        break;
                    }
                default:
                    isCan = inIsCan;
                    break;
            }
        }


        private void FixedType(List<JobTime> jobTimeList , List<string> newTime, JobTime item, bool inIsCan, out bool isCan)
        {
            isCan = inIsCan;
            switch (item.Index)
            {
                case 0:
                    {
                        if (item.JobRunTime == newTime[item.Index]
                            && newTime[minute] == jobTimeList[minute].JobRunTime && newTime[seconds] == jobTimeList[seconds].JobRunTime)
                        {
                            isCan = true;
                        }
                        break;
                    }
                case 1:
                    {
                        if (item.JobRunTime == newTime[item.Index]
                            && newTime[seconds] == jobTimeList[seconds].JobRunTime)
                        {
                            isCan = true;
                        }
                        break;
                    }
                case 2:
                    {
                        if (item.JobRunTime == newTime[item.Index])
                        {
                            isCan = true;
                        }
                        break;
                    }
            }
        }


        public  bool IsWeek(string jobWeek)
        {
            List<string> week = jobWeek.Split(',').ToList();
            return week.Exists(x => x == JsonConvert.SerializeObject(DateTime.Now.DayOfWeek));
        }

    }
}
